package com.woniu.mapper;

import com.woniu.domain.Permission;
import com.woniu.domain.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import com.woniu.vo.UserAllInterviewsVo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zyy
 * @since 2021-03-05
 */
public interface UserMapper extends BaseMapper<User> {
    //注册时给用户角色
    @Insert("INSERT INTO t_user_role(uid,rid) VALUES(#{uid},#{rid})")
    public void setRoleByUid(Integer uid,Integer rid);

    //修改用户角色
    @Update("UPDATE t_user_role SET rid=#{rid} WHERE uid=#{uid}")
    public void updateRole(Integer rid,Integer uid);



//    1求职者面试情况
//    1.1全部面试
    @Select("SELECT u.username AS username,\n" +
            "c.`name` AS companyName,\n" +
            "p.tname AS positionName,\n" +
            "f.`status` AS feedbackStatus\n" +
            "FROM t_user AS u\n" +
            "JOIN t_feedback AS f\n" +
            "ON u.id=f.user_id\n" +
            "JOIN t_user_position AS up\n" +
            "ON f.position_id=up.pid\n" +
            "JOIN t_position AS p\n" +
            "ON up.pid=p.position_id\n" +
            "JOIN t_user_company AS uc\n" +
            "ON up.uid=uc.user_id\n" +
            "JOIN t_company AS c\n" +
            "ON uc.company_id=c.company_id\n" +
            "WHERE u.id=#{userId}")
    public List<UserAllInterviewsVo> interviewManagementData(Integer userId);


//    1.2 已接受面试
    @Select("SELECT u.username AS username,\n" +
            "c.`name` AS companyName,\n" +
            "p.tname AS positionName,\n" +
            "f.`status` AS feedbackStatus\n" +
            "FROM t_user AS u\n" +
            "JOIN t_feedback AS f\n" +
            "ON u.id=f.user_id\n" +
            "JOIN t_user_position AS up\n" +
            "ON f.position_id=up.pid\n" +
            "JOIN t_position AS p\n" +
            "ON up.pid=p.position_id\n" +
            "JOIN t_user_company AS uc\n" +
            "ON up.uid=uc.user_id\n" +
            "JOIN t_company AS c\n" +
            "ON uc.company_id=c.company_id\n" +
            "WHERE u.id=#{userId}\n" +
            "AND f.`status`='已接受' ")
    public List<UserAllInterviewsVo> accepted(Integer userId);

//    1.3 待处理

    @Select("SELECT u.username AS username,\n" +
            "c.`name` AS companyName,\n" +
            "p.tname AS positionName,\n" +
            "f.`status` AS feedbackStatus\n" +
            "FROM t_user AS u\n" +
            "JOIN t_feedback AS f\n" +
            "ON u.id=f.user_id\n" +
            "JOIN t_user_position AS up\n" +
            "ON f.position_id=up.pid\n" +
            "JOIN t_position AS p\n" +
            "ON up.pid=p.position_id\n" +
            "JOIN t_user_company AS uc\n" +
            "ON up.uid=uc.user_id\n" +
            "JOIN t_company AS c\n" +
            "ON uc.company_id=c.company_id\n" +
            "WHERE u.id=#{userId}\n" +
            "AND f.`status`='待处理' ")
    public List<UserAllInterviewsVo> pendingProcessing(Integer userId);


    //    1.4已拒绝

    @Select("SELECT u.username AS username,\n" +
            "c.`name` AS companyName,\n" +
            "p.tname AS positionName,\n" +
            "f.`status` AS feedbackStatus\n" +
            "FROM t_user AS u\n" +
            "JOIN t_feedback AS f\n" +
            "ON u.id=f.user_id\n" +
            "JOIN t_user_position AS up\n" +
            "ON f.position_id=up.pid\n" +
            "JOIN t_position AS p\n" +
            "ON up.pid=p.position_id\n" +
            "JOIN t_user_company AS uc\n" +
            "ON up.uid=uc.user_id\n" +
            "JOIN t_company AS c\n" +
            "ON uc.company_id=c.company_id\n" +
            "WHERE u.id=#{userId}\n" +
            "AND f.`status`='已拒绝'")
    public List<UserAllInterviewsVo> hasBeenRejected(Integer userId);




//2.我的申请
//    2.1 已申请

    @Select("SELECT u.username AS username,\n" +
            "c.`name` AS companyName,\n" +
            "p.tname AS positionName,\n" +
            "f.`status` AS feedbackStatus\n" +
            "FROM t_user AS u\n" +
            "JOIN t_feedback AS f\n" +
            "ON u.id=f.user_id\n" +
            "JOIN t_user_position AS up\n" +
            "ON f.position_id=up.pid\n" +
            "JOIN t_position AS p\n" +
            "ON up.pid=p.position_id\n" +
            "JOIN t_user_company AS uc\n" +
            "ON up.uid=uc.user_id\n" +
            "JOIN t_company AS c\n" +
            "ON uc.company_id=c.company_id\n" +
            "WHERE u.id=#{userId}\n" +
            "AND f.`status`='已拒绝' \n" +
            " OR f.`status`='待处理'")
    public List<UserAllInterviewsVo> hasApplied(Integer userId);


    //2.我的申请
//    2.2 我的收藏


    @Select("SELECT u.id AS id,\n" +
            "       u.username AS username,\n" +
            "       t_company.`name` AS companyName,\n" +
            "       t_position.`tname` AS positionName,\n" +
            "     t_position.position_id AS positionId  \n" +
            "FROM\n" +
            "t_collect AS cp JOIN t_user AS u ON cp.user_id=u.id \n" +
            "JOIN t_company ON t_company.company_id = cp.company_id\n" +
            "JOIN t_position ON t_position.position_id = cp.position_id\n" +
            "\n" +
            "WHERE u.id=#{userId}")

    public List<UserAllInterviewsVo> myCollection(Integer userId);


//    我的收藏新增功能

    @Insert("INSERT INTO t_collect(company_id,position_id,user_id) VALUES(#{companyId},#{positionId},#{userId})")
    public int insertCollection(Integer companyId,Integer positionId,Integer userId);




//    支付宝修改用户类型
    @Update("UPDATE t_user SET vip=\"vip\" WHERE id=#{uid}")
    public void vip(Integer uid);

    @Select("select p.*\n" +
            "from t_user u\n" +
            "join t_user_role ur\n" +
            "on u.id=ur.uid\n" +
            "join t_role r\n" +
            "on r.role_id=ur.rid\n" +
            "join t_role_permission rp \n" +
            "on rp.rid=r.role_id\n" +
            "join t_permission p\n" +
            "on p.id=rp.pid\n" +
            "where u.id=#{uid}")
    List<Permission> getMenus(Integer uid);




}
